<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{
	public $username;
	public $password;
	public $rememberMe;
	private $_identity;
	public  $check;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('username, password', 'required'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// password needs to be authenticated
			array('password', 'authenticate'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'rememberMe'=>'Remember me next time',
		);
	}
	
	public function setPassword($pass){
		$this->password = $pass;
	}
	public function setUsername($user){
		$this->username = $user;
	}
	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$identity = new UserIdentity($this->username,$this->password);
			$identity->authenticate();
			switch($identity->errorCode) {
				case UserIdentity::ERROR_NONE:
					$duration = $this->rememberMe ? 3600*24*30 : 0; //30 days
					Yii::app()->user->login($identity, $duration);
					break;
				case UserIdentity::ERROR_USERNAME_INVALID:
					Yii::app()->user->setFlash('username', "Username is incorrect.");
					$this->addError('username', 'Username is incorrect.');
					break;
				case UserIdentity::ERROR_PASSWORD_INVALID:
					$this->addError('password', 'Password is incorrect.');
					Yii::app()->user->setFlash('password', "Password is incorrect.");
					break;
			}
		}
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		$user_id = Yii::app()->user->id;
		$userprofiles = UserProfiles::model()->findByAttributes(array('user_id'=>$user_id));
		if(!isset($userprofiles->user_id)) {
			// an SQL with two placeholders ":username" and ":email"
			$sql="INSERT INTO tbl_user_profiles (user_id, display_name) VALUES(:user_id, :display_name)";	
			$command = Yii::app()->db->createCommand($sql);
			// replace the placeholder ":username" with the actual username value
			$command->bindParam(":user_id",$user_id,PDO::PARAM_INT);
			// replace the placeholder ":email" with the actual email value
			$command->bindParam(":display_name",$this->username,PDO::PARAM_STR);		
			$command->execute();
		}
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else
			return false;
	}
}
